<?php
namespace app\controller\manager;

use app\BaseController;
use app\model\Teacher;
use app\model\VTeacherInfo;
use think\Request;
use \think\facade\Db;

/**
 * 教师管理控制器
 * @todo 完善增删改查
 * @todo 完善excel导入
 */
class TeacherManage extends BaseController
{

    public function list(){
        $pageSize = $this->request->param("pageSize");
        $TID = $this->request->param("TID" , "not null");
        $job = $this->request->param("job" , "not null");
        $academyID = $this->request->param('academyID','not null');
        $teacher = VTeacherInfo::where('academyID',$academyID)
                                ->where('TID',$TID)
                                ->where('job',$job)
                                ->paginate($pageSize);
        return resultSuccess($teacher);
    }

    public function getTeacherName(){
        $TID = $this->request->param('TID');
        $teacher = VTeacherInfo::field(['name','academyName'])->where('TID',$TID)->find();
        if($teacher == null){
            return resultError('无此教师');
        }
        return resultSuccess($teacher);
    }

    /**
     * 增加教师
     */
    public function addTeacher(Request $request)
    {
        if ($request->isPost()) {
            $teacher = new Teacher;
            if ($teacher->save(
                [
                    'TID' => $request->param('TID'),
                    'academyID' => $request->param('academyID'),
                    'name' => $request->param('name'),
                    'job' => $request->param('job'),
                    'password' => \password_hash($request->param('TID'),PASSWORD_DEFAULT)
                ]
            )) {
                return resultSuccess(null,"成功");
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 删除教师
     */
    public function deleteTeacher(Request $request)
    {
        if ($request->isGet()) {
            $teacher = Teacher::find($request->param('TID'));
            if ($teacher->delete()) {
                return resultSuccess(null,"成功");
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 修改教师信息
     */
    public function editTeacher(Request $request)
    {
        if ($request->isPost()) {
            $teacher = Teacher::find($request->param('TID'));
            if ($teacher->save(
                [
                    'academyID' => $request->param('academyID'),
                    'name' => $request->param('name'),
                    'job' => $request->param('job'),
                ]
            )) {
                return resultSuccess(null,"成功");
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }
}
